package mrms.api.web.config;

import lombok.AllArgsConstructor;
import mrms.api.repository.DeptRepository;
import mrms.api.repository.UserRepository;
import mrms.api.web.authentication.Authentication;
import mrms.api.web.authentication.AuthenticationInterceptor;
import mrms.api.web.authentication.JwtUtils;
import mrms.api.web.interceptors.AdminPermissionInterceptor;
import mrms.api.web.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author lenchu
 * @date 2019/01/31
 */
@Configuration
@AllArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {
    private final UserRepository userRepository;
    private final JwtUtils jwtUtils;
    private final DeptRepository deptRepository;

    private final List<Authentication> authentications;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor(authentications))
                .addPathPatterns("/login");
        registry.addInterceptor(new LoginInterceptor(userRepository, jwtUtils, deptRepository))
                .addPathPatterns("/**")
                .excludePathPatterns("/login", "/reg", "/dept/**", "/download", "/front/**","/user/getcode/*","/user/verify","/user/forgetpwd");
        registry.addInterceptor(new AdminPermissionInterceptor()).addPathPatterns("/a/**");
    }
}
